package com.bridgeintelligent.tag.combination.service;

import com.bridgeintelligent.tag.combination.entity.CombinationPage;
import com.bridgeintelligent.tag.combination.mapper.CombinationMapper;
import com.bridgeintelligent.tag.combination.model.CombinationStopModel;
import com.bridgeintelligent.tag.combination.model.CombinationStopQueryModel;
import com.bridgeintelligent.tag.combination.pojo.Combination;
import com.bridgeintelligent.tag.customer.exception.CustomersException;
import com.bridgeintelligent.tag.utils.DateHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wayneleo.quickstart.framework.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description
 * @author: grp 2021-03-03 21:33
 **/
@Service
@AllArgsConstructor
@Slf4j
public class CombinationStopServiceImpl implements ICombinationStopService {

    private final CombinationMapper combinationMapper;
    private SqlSessionTemplate sqlSessionTemplate;
    private CombinationService combinationService;

    @Override
    public CombinationPage<Combination> list(CombinationStopQueryModel model) {
        Page page = PageHelper.startPage(model.getPageNumber(), model.getPageSize());
        List<Combination> list = combinationMapper.findByDimId(model);
        if (list != null && !list.isEmpty()) {
            for (Combination combination : list) {
                if ("1".equals(combination.getApprovalStatus())) {
                    combination.setStatusName("未提交");
                } else if ("2".equals(combination.getApprovalStatus())) {
                    combination.setApprovalStatusName("待审核");
                } else if ("3".equals(combination.getApprovalStatus())) {
                    combination.setApprovalStatusName("驳回");
                } else if ("4".equals(combination.getApprovalStatus())) {
                    combination.setApprovalStatusName("已通过");
                }
            }
        }
        CombinationPage<Combination> combinationPage = new CombinationPage<>();
        combinationPage.setContent(list);
        combinationPage.setTotalPages(page.getPages());
        combinationPage.setTotalElements(page.getTotal());
        combinationPage.setSize(page.getPageSize());
        combinationPage.setRequestPager(model.getPageNumber());
        return combinationPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CombinationStopModel model) {
        if(model.isAll()){
            if(StringUtils.isBlank(model.getDimId())){
                throw new CustomersException(new ExceptionCode(999, "批量失败！"));
            }
            List<Combination> list = combinationMapper.findByDimId(new CombinationStopQueryModel().setDimId(model.getDimId()));
            list.forEach(c->combinationService.delCombination(c.getCombinationId()));
            combinationMapper.stopCustomers(DateHelper.currentDateTime());
            return;
        }
        if(!CollectionUtils.isEmpty(model.getIds())){
            model.getIds().forEach(id->combinationService.delCombination(id));
            combinationMapper.stopCustomers(DateHelper.currentDateTime());
        }
    }
}
